package com.sudichina.sudichina.view.activity.releaseline;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.TTranLineSeviceVO;
import com.sudichina.sudichina.entity.ReleaseRoute;
import com.sudichina.sudichina.querydatabase.Citys;
import com.sudichina.sudichina.querydatabase.DBManager;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.LineManageActivity;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sudichina.sudichina.R.id.tv_fcl_unit;

public class FCLPriceActivity extends AppCompatActivity implements onRequestDataFinish, View.OnClickListener, TextWatcher {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_fcl_price_mylocation)
    TextView tvFclPriceMylocation;
    @BindView(R.id.tv_fcl_price_tolocation)
    TextView tvFclPriceTolocation;
    @BindView(tv_fcl_unit)
    TextView tvFclUnit;
    @BindView(R.id.tv_flc_service_price)
    TextView tvFlcServicePrice;


    @BindView(R.id.et_stationtodoor)
    EditText etStationtodoor;
    @BindView(R.id.et_doortostation)
    EditText etDoortostation;
    @BindView(R.id.et_doortodoor)
    EditText etDoortodoor;
    @BindView(R.id.et_stationostation)
    EditText etStationostation;
    @BindView(R.id.bt_fcl_confim)
    Button btFclConfim;
    @BindView(R.id.activity_all_call_price)
    LinearLayout activityAllCallPrice;
    @BindView(R.id.et_all_car_price)
    EditText privateEtAllCarPrice;

    private AlertDialog chooseWeight;
    private String starting;
    private String destination;
    private String license;
    private String mode;
    private String starttime;
    private String overtime;
    private int carId;
    private int dicId;

    //斤方吨用到的变量
    private final int DIALOG_JIN = 1001;
    private final int DIALOG_FANG = 1002;
    private final int DIALOG_DUN = 1003;
    private int DIALOG_CURRENTPOSITION = DIALOG_JIN;
    private String STR_DIALOG_JIN = "";
    private String STR_DIALOG_FANG = "";
    private String STR_DIALOG_DUN = "";


    private TextView tvJin;
    private TextView tvFang;
    private TextView tvDun;
    private EditText etPrice;
    private boolean clearAvidListern = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fcl_price);
        ButterKnife.bind(this);
        showMyDialog();
        tvTitle.setText("整车报价");
        initIntent();
// 设置传入的目的地
        tvFclPriceTolocation.setText(destination);


    }

    private void initIntent() {
        Intent intent = getIntent();
        try {
            //出发地
            starting = (String) intent.getCharSequenceExtra("starting");
            //目的地
            destination = (String) intent.getCharSequenceExtra("destination");
            //车牌
            license = (String) intent.getCharSequenceExtra("license");
            //模式
            mode = (String) intent.getCharSequenceExtra("mode");
            //运期 开始时间
            starttime = (String) intent.getCharSequenceExtra("starttime");

            overtime = (String) intent.getCharSequenceExtra("overtime");
            carId = intent.getIntExtra("carId", 0);
            dicId = intent.getIntExtra("dicId", 0);
            tvFclPriceMylocation.setText(starting + "(我的位置)");
        } catch (Exception e) {
            Toastutils.showToast("Exception" + e);
        }


    }


    private void showMyDialog() {

        chooseWeight = new AlertDialog.Builder(this).create();
        View view = View.inflate(this, R.layout.dialog_route, null);
        TextView tvXzCity = (TextView) view.findViewById(R.id.tv_xz_city);
        tvJin = (TextView) view.findViewById(R.id.tv_jin);
        tvFang = (TextView) view.findViewById(R.id.tv_fang);
        tvDun = (TextView) view.findViewById(R.id.tv_dun);
        etPrice = (EditText) view.findViewById(R.id.et_price);
        TextView tvConfrim = (TextView) view.findViewById(R.id.tv_cancel);
        TextView tvcancel = (TextView) view.findViewById(R.id.tv_confirm);
        //城市输入上
        Toastutils.showToast("斤,方,吨,三个单价必须全部输入");
        tvXzCity.setText(destination);
        tvJin.setOnClickListener(this);
        tvFang.setOnClickListener(this);
        tvDun.setOnClickListener(this);
        tvConfrim.setOnClickListener(this);
        tvcancel.setOnClickListener(this);
        etPrice.addTextChangedListener(this);
/*
*
*     chooseWeight.getWindow().setContentView(layout);
    chooseWeight.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    chooseWeight.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
            WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
* */
        chooseWeight.show();
        Window window = chooseWeight.getWindow();
        WindowManager.LayoutParams layoutparams = new WindowManager.LayoutParams();
        layoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutparams.width = (int) getResources().getDimension(R.dimen.x720);
        window.setAttributes(layoutparams);
        window.setContentView(view);
//        chooseWeight.setContentView(view);
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


    }


    @Override
    public void requestdataFinish(String catalog, Object bean) {
        String bean1 = (String) bean;
//判断行程是否发布成功  路线发布成功
        if (bean1.contains("路线发布成功")) {
            Toastutils.showToast("路线发布成功");

            //跳转线路管理的界面
            Intent intent = new Intent(this, LineManageActivity.class);

            startActivity(intent);
            finish();
        }else {
            Toastutils.showToast("路线发布失败,已有发布的行程");
        }


    }

    @Override
    public void requestdataFailed(IOException e) {

    }

    @OnClick({R.id.iv_back, R.id.bt_fcl_confim, R.id.tv_fcl_unit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_fcl_unit:
                showMyDialog();
                break;
            case R.id.tv_jin:
                //清空的时候免除监听
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入斤价格");
                clearAvidListern = false;

                DIALOG_CURRENTPOSITION = DIALOG_JIN;
                tvJin.setTextColor(Color.parseColor("#ffffff"));
                tvJin.setBackgroundColor(Color.parseColor("#439198"));
                tvFang.setTextColor(Color.parseColor("#000000"));
                tvFang.setBackgroundColor(Color.parseColor("#ffffff"));
                tvDun.setTextColor(Color.parseColor("#000000"));
                tvDun.setBackgroundColor(Color.parseColor("#ffffff"));


                break;
            case R.id.tv_fang:
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入方价格");
                clearAvidListern = false;
                DIALOG_CURRENTPOSITION = DIALOG_FANG;

                tvFang.setTextColor(Color.parseColor("#ffffff"));
                tvFang.setBackgroundColor(Color.parseColor("#439198"));
                tvJin.setTextColor(Color.parseColor("#000000"));
                tvJin.setBackgroundColor(Color.parseColor("#ffffff"));
                tvDun.setTextColor(Color.parseColor("#000000"));
                tvDun.setBackgroundColor(Color.parseColor("#ffffff"));
                break;
            case R.id.tv_dun:
                clearAvidListern = true;
                etPrice.setText("");
                //设置提示
                etPrice.setHint("请输入吨价格");
                clearAvidListern = false;
                DIALOG_CURRENTPOSITION = DIALOG_DUN;
                tvDun.setTextColor(Color.parseColor("#ffffff"));
                tvDun.setBackgroundColor(Color.parseColor("#439198"));
                tvFang.setTextColor(Color.parseColor("#000000"));
                tvFang.setBackgroundColor(Color.parseColor("#ffffff"));
                tvJin.setTextColor(Color.parseColor("#000000"));
                tvJin.setBackgroundColor(Color.parseColor("#ffffff"));

                break;
            case R.id.tv_cancel:
                chooseWeight.dismiss();
                break;
            case R.id.tv_confirm:
                if ("".equals(STR_DIALOG_JIN)) {
                    Toastutils.showToast("斤未设置");
                    return;
                }
                if ("".equals(STR_DIALOG_FANG)) {
                    Toastutils.showToast("方未设置");
                    return;
                }
                if ("".equals(STR_DIALOG_DUN)) {
                    Toastutils.showToast("吨未设置");
                    return;
                }
                tvFclUnit.setText(STR_DIALOG_JIN + "元/斤  " + STR_DIALOG_FANG + "元/方  " + STR_DIALOG_DUN + "元/吨  ");
                chooseWeight.dismiss();
                break;
            case R.id.iv_back:
                finish();
                break;

            case R.id.bt_fcl_confim:

/**
 * Double priceDun; // 吨的单价
 Double priceSquare; // 方的单价
 Double priceKg; // 千克的单价
 Double priceAll; // 整车价格

 Double prickDoor; // 千克的单价
 Double prickStation; // 整车价格

 Date releaseTime; // 发车时间
 Date arriveTime; // 到达时间
 Short sendTime;// 短信发送次数
 Date sendDate;// 最近一次短信发送时间
 Date pushDate; // 最后一次推送日期
 Short pushTime; // 推送次数
 Short level; // 渠道等级 1分通公司，2大区级，3省级，4地市级
 Boolean isUse; // 是否发展了渠道商 0:没有 1有
 String addService; // 以逗号隔开的服务编码
 Boolean isLock; // 是否锁定 0:未锁定 1:已锁定
 Integer status;

 private String starting;
 private String destination;

 private String mode;
 private String starttime;
 private String overtime;
 private String carId;
 private String dicId;


 */
                String trim = privateEtAllCarPrice.getText().toString().trim();
                if (trim == null) {
                    Toastutils.showToast("整车报价未未设置");
                    return;
                }
//查询开始区域和结束区域的编码
                ReleaseRoute releaseRoute = new ReleaseRoute();
                DBManager dbManager = new DBManager(this);


                Citys cityName = dbManager.getCityFromCityName(starting);

                Citys cityName1 = dbManager.getCityFromCityName(destination);


                if (cityName != null) {

                    releaseRoute.setFromZoneCode(cityName.getId());
                    System.out.println("开始区域的编码:" + cityName.getId());
                }
                if (cityName1 != null) {

                    releaseRoute.setToZoneCode(cityName1.getId());
                    System.out.println("开始区域的编码:" + cityName1.getId());
                }


                releaseRoute.setAccountId(String.valueOf(MyApp.getCurrentUser().getId()));
                releaseRoute.setCarId(String.valueOf(carId));
                releaseRoute.setDicId(String.valueOf(dicId));

                releaseRoute.setType(null);
                releaseRoute.setFromAddress(starting);

                releaseRoute.setPriceDun(STR_DIALOG_DUN);
                releaseRoute.setPriceSquare(STR_DIALOG_FANG);
                releaseRoute.setPriceKg(STR_DIALOG_JIN);

                releaseRoute.setPriceAll(trim);//价格整车
                releaseRoute.setReleaseTime(starttime);
                releaseRoute.setArriveTime(overtime);

                TTranLineSeviceVO tTranLineSeviceVO = new TTranLineSeviceVO();


                tTranLineSeviceVO.setServiceType(String.valueOf(1));

                tTranLineSeviceVO.setServicePrice("500");


                ArrayList<TTranLineSeviceVO> list = new ArrayList<TTranLineSeviceVO>();
                list.add(tTranLineSeviceVO);
                releaseRoute.setTlist(list);

                releaseRoute.setTtLinelist(null);

                NetUtils.requestDataformentity(Api.RELEASELINEASAPP, releaseRoute, this, null);

                break;


        }
    }

    /**
     * EditText 监听
     *
     * @param s
     * @param start
     * @param count
     * @param after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//        Toastutils.showToast("beforeTextChanged" + s);
    }


    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
//        Toastutils.showToast("onTextChanged" + s);
    }

    @Override
    public void afterTextChanged(Editable s) {
//        Toastutils.showToast("afterTextChanged" + s);
        if (clearAvidListern) {

        } else {
            switch (DIALOG_CURRENTPOSITION) {
                case DIALOG_JIN:
                    STR_DIALOG_JIN = s.toString();
                    break;
                case DIALOG_FANG:
                    STR_DIALOG_FANG = s.toString();
                    break;
                case DIALOG_DUN:
                    STR_DIALOG_DUN = s.toString();
                    break;
            }
        }
    }
}

